﻿// ===============================================================================
// DatabaseSchema.cs
// EOS Framework
// ===============================================================================
// Copyright (c) EOS Development Group. All rights reserved.
// ===============================================================================

namespace EOS.Framework.Contracts.Schema
{
    using System;
    using EOS.Framework.Tools;

    /// <summary>
    /// Provides Schema Information about a database.
    /// </summary>
    [Serializable]
    public class DatabaseSchema : IEquatable<DatabaseSchema>
    {
        #region Properties

        /// <summary>
        /// The connection string.
        /// </summary>
        private string _connectionString;

        /// <summary>
        /// Gets or sets the connection string.
        /// </summary>
        /// <value>The connection string.</value>
        public string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }

        /// <summary>
        /// List of all tables in the database.
        /// </summary>
        private TableSchemaCollection _tables;

        /// <summary>
        /// Gets the list of all table schemas.
        /// </summary>
        /// <value>The tables.</value>
        public TableSchemaCollection Tables
        {
            get
            {
                if (_tables == null)
                {
                    _tables = new TableSchemaCollection();
                }
                return _tables;
            }
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="DatabaseSchema"/> class.
        /// </summary>
        public DatabaseSchema()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DatabaseSchema"/> class with
        /// the connection string.
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        public DatabaseSchema(string connectionString)
        {
            this._connectionString = connectionString;
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal 
        /// to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with 
        /// the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the 
        /// current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            return Equals(obj as DatabaseSchema);
        }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        public bool Equals(DatabaseSchema other)
        {
            if (other == null)
            {
                return false;
            }

            bool result = true;

            if (!Object.Equals(_connectionString, other._connectionString))
            {
                result = false;
            }

            if (!Object.Equals(_tables, other._tables))
            {
                result = false;
            }

            return result;
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            int hashCode = 1;
            
            if (_connectionString != null)
            {
                _connectionString.GetHashCode();
            }

            if (_tables != null)
            {
                hashCode ^= _tables.CreateHashCode();
            }

            return hashCode;
        }
    }
}
